package com.yanqu.road.server.cmd.vehicle;

import com.yanqu.road.logic.pb.VehiclePb;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.pb.vehicle.VehicleProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_VEHICLE_STUDY, desc = "座骑学习，拓展技能")
public class VehicleStudyCmd extends AbstractCommand {

	private String vehicleUid;

	private int type;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		VehicleProto.VehicleStudyReqMsg reqMsg = VehicleProto.VehicleStudyReqMsg.parseFrom(packet.getBytes());
		vehicleUid = reqMsg.getVehicleUid();
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ret = player.getModule(VehicleModule.class).vehicleStudy(type, vehicleUid);
	}

	@Override
	public void sendData(GamePlayer player) {
		VehicleProto.VehicleStudyRespMsg.Builder respMsg = VehicleProto.VehicleStudyRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(vehicleUid);
			VehicleProto.VehicleTempMsg.Builder vehicleMsg = VehiclePb.parseVehicleTempMsg(userVehicle);
			if(null != vehicleMsg){
				respMsg.setVehicle(vehicleMsg);
			}
		}
		player.sendPacket(Protocol.U_VEHICLE_STUDY , respMsg);
	}

}
